package breeze.pixel.weather.adapter;

import android.annotation.SuppressLint;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qweather.sdk.bean.IndicesBean;
import com.qweather.sdk.bean.WarningBean;
import com.qweather.sdk.bean.air.AirNowBean;
import com.qweather.sdk.bean.base.Code;
import com.qweather.sdk.bean.base.IndicesType;
import com.qweather.sdk.bean.base.Lang;
import com.qweather.sdk.bean.base.Range;
import com.qweather.sdk.bean.geo.GeoBean;
import com.qweather.sdk.bean.sunmoon.MoonBean;
import com.qweather.sdk.bean.weather.WeatherDailyBean;
import com.qweather.sdk.bean.weather.WeatherHourlyBean;
import com.qweather.sdk.bean.weather.WeatherNowBean;
import com.qweather.sdk.view.QWeather;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import breeze.pixel.weather.App;
import breeze.pixel.weather.adapter.weather.AirNowAdapter;
import breeze.pixel.weather.adapter.weather.CityAdapter;
import breeze.pixel.weather.adapter.weather.DailyWeatherAdapter;
import breeze.pixel.weather.adapter.weather.HourlyAdapter;
import breeze.pixel.weather.adapter.weather.IndicesAdapter;
import breeze.pixel.weather.adapter.weather.MoonAdapter;
import breeze.pixel.weather.adapter.weather.WarnAdapter;
import breeze.pixel.weather.adapter.weather.WeatherNowAdapter;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;

public class HeFengWeatherAPI implements IAppWeatherHelper {
    @Override
    public void getWeatherNow(String cityID, NowWeatherResponse response) {
        QWeather.getWeatherNow(App.getContext(),cityID, new QWeather.OnResultWeatherNowListener() {
            @Override
            public void onError(Throwable throwable) {
                throwable.printStackTrace();
            }

            @Override
            public void onSuccess(WeatherNowBean weatherNowBean) {
                if (weatherNowBean.getCode().getCode().equalsIgnoreCase(Code.NO_MORE_REQUESTS.getCode())) {
                    AppSettings appSettings = App.getAppSettings();
                    int defaultKey = appSettings.getDefaultKey();
                    if (defaultKey == 0) {
                        defaultKey = 1;
                    } else if (defaultKey == 1) {
                        defaultKey = 0;
                    }
                    appSettings.setDefaultKey(defaultKey);
                    AppToolUtil.sendBroadcast(App.getContext(), AppSettings._ACTION_UPDATE_DEFAULT_HEWEATHER_KEY);
                } else {
                    WeatherNowBean.NowBaseBean now = weatherNowBean.getNow();
                    try {
                        Gson gson = new Gson();
                        String s = gson.toJson(now);
                        WeatherNowAdapter.NowBean nowBean = gson.fromJson(s, WeatherNowAdapter.NowBean.class);
                        WeatherNowAdapter adapter = new WeatherNowAdapter();
                        adapter.setUpdateTime(weatherNowBean.getBasic().getUpdateTime().replace("+08:00", "").replace("T", " "));
                        adapter.setNowBean(nowBean);
                        response.success(adapter);
                    } catch (Exception e) {
                        e.printStackTrace();
                        response.fail(e.toString());
                    }
                }
            }
        });
    }

    @Override
    public void getAirQuality(String cityID, AirQualityResponse response) {
        QWeather.getAirNow(App.getContext(),cityID,Lang.ZH_HANS,new QWeather.OnResultAirNowListener(){
            @Override
            public void onError(Throwable throwable) {
                response.fail(throwable.toString());
            }

            @Override
            public void onSuccess(AirNowBean airNowBean) {
                AirNowBean.NowBean now = airNowBean.getNow();
                Gson gson = new Gson();
                String s = gson.toJson(now);
                AirNowAdapter.NowBean nowBean = gson.fromJson(s, AirNowAdapter.NowBean.class);
                AirNowAdapter airNowAdapter = new AirNowAdapter();
                airNowAdapter.setNowBean(nowBean);
                response.success(airNowAdapter);
            }
        });
    }

    @Override
    public void getWeatherWarn(String cityID, WeatherWarnResponse response) {
        QWeather.getWarning(App.getContext(), cityID, new QWeather.OnResultWarningListener() {
            @Override
            public void onError(Throwable throwable) {
                response.fail(throwable.toString());
            }

            @Override
            public void onSuccess(WarningBean warningBean) {
                if (warningBean.getWarningList().size()>0) {
                    WarningBean.WarningBeanBase warningBeanBase = warningBean.getWarningList().get(0);
                    Gson gson = new Gson();
                    String s = gson.toJson(warningBeanBase);
                    WarnAdapter.WarningBeanBase warningBeanBase1 = gson.fromJson(s, WarnAdapter.WarningBeanBase.class);
                    WarnAdapter data = new WarnAdapter();
                    data.setBean(warningBeanBase1);
                    response.success(data);
                } else {
                    response.success(null);
                }
            }
        });
    }

    @Override
    public void getHourlyWeather(String cityID, HourlyWeatherResponse response) {
        QWeather.getWeather24Hourly(App.getContext(), cityID, new QWeather.OnResultWeatherHourlyListener() {
            @Override
            public void onError(Throwable throwable) {
                response.fail(throwable.toString());
            }

            @Override
            public void onSuccess(WeatherHourlyBean weatherHourlyBean) {
                List<WeatherHourlyBean.HourlyBean> hourly = weatherHourlyBean.getHourly();
                Gson gson = new Gson();
                String s = gson.toJson(hourly);
                List<HourlyAdapter.HourlyBean> beans = gson.fromJson(s, new TypeToken<List<HourlyAdapter.HourlyBean>>(){}.getType());
                HourlyAdapter hourlyAdapter = new HourlyAdapter();
                hourlyAdapter.setHourly(beans);
                response.success(hourlyAdapter);
            }
        });
    }

    @Override
    public void getIndices(String cityID, IndicesResponse response) {
        List<IndicesType> types = new ArrayList<>();
        types.add(IndicesType.ALL);
        QWeather.getIndices1D(App.getContext(),cityID,Lang.ZH_HANS,types,new QWeather.OnResultIndicesListener(){
            @Override
            public void onError(Throwable throwable) {
                response.fail(throwable.toString());
            }

            @Override
            public void onSuccess(IndicesBean indicesBean) {
                List<IndicesBean.DailyBean> dailyList = indicesBean.getDailyList();
                Gson gson = new Gson();
                String s = gson.toJson(dailyList);
                List<IndicesAdapter.DailyBean> dailyBeans = gson.fromJson(s,new TypeToken<List<IndicesAdapter.DailyBean>>(){}.getType());
                IndicesAdapter indicesAdapter = new IndicesAdapter();
                indicesAdapter.setDailyBeans(dailyBeans);
                response.success(indicesAdapter);
            }
        });
    }

    @Override
    public void getMoonInfo(String cityID, MoonInfoResponse response) {
        @SuppressLint("SimpleDateFormat") String date = new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis());
        QWeather.getMoon(App.getContext(),cityID,date,new QWeather.OnResultMoonListener(){
            @Override
            public void onError(Throwable throwable) {
                response.fail(throwable.toString());
            }

            @Override
            public void onSuccess(MoonBean moonBean) {
                Gson gson = new Gson();
                MoonBean.MoonPhaseBean moonPhaseBean = moonBean.getMoonPhaseBeanList().get(0);
                String s = gson.toJson(moonPhaseBean);
                MoonAdapter.MoonPhaseBean moonPhaseBean1 = gson.fromJson(s, MoonAdapter.MoonPhaseBean.class);
                MoonAdapter moonAdapter = new MoonAdapter();
                moonAdapter.setBean(moonPhaseBean1);
                moonAdapter.setMoonrise(moonBean.getMoonrise());
                moonAdapter.setMoonset(moonBean.getMoonset());
                response.success(moonAdapter);
            }
        });
    }

    @Override
    public void getDailyWeather(String cityID, int days, DailyWeatherResponse response) {
        QWeather.OnResultWeatherDailyListener onResultWeatherDailyListener = new QWeather.OnResultWeatherDailyListener() {
            @Override
            public void onError(Throwable throwable) {
                response.fail(throwable.toString());
            }

            @Override
            public void onSuccess(WeatherDailyBean weatherDailyBean) {
                List<WeatherDailyBean.DailyBean> daily = weatherDailyBean.getDaily();
                Gson gson = new Gson();
                String s = gson.toJson(daily);
                List<DailyWeatherAdapter.DailyBean> beans = gson.fromJson(s,new TypeToken<List<DailyWeatherAdapter.DailyBean>>(){}.getType());
                DailyWeatherAdapter dailyWeatherAdapter = new DailyWeatherAdapter();
                dailyWeatherAdapter.setDaily(beans);
                response.success(dailyWeatherAdapter);
            }
        };

        if (days == 3) {
            QWeather.getWeather3D(App.getContext(),cityID,onResultWeatherDailyListener);
        } else if (days == 15) {
            QWeather.getWeather15D(App.getContext(),cityID,onResultWeatherDailyListener);
        }
    }

    @Override
    public void getCityInfo(String name, CityQueryResponse response) {
        QWeather.getGeoCityLookup(App.getContext(), name, Range.CN, 20, Lang.ZH_HANS, new QWeather.OnResultGeoListener() {
            @Override
            public void onError(Throwable throwable) {
                response.fail("搜索城市失败：" + throwable.toString());
            }

            @Override
            public void onSuccess(GeoBean geoBean) {
                if (Code.OK == geoBean.getCode()) {
                    List<GeoBean.LocationBean> locationBean = geoBean.getLocationBean();
                    Gson gson = new Gson();
                    String s = gson.toJson(locationBean);
                    List<CityAdapter.LocationBean> beans = gson.fromJson(s, new TypeToken<List<CityAdapter.LocationBean>>() {
                    }.getType());
                    CityAdapter cityAdapter = new CityAdapter();
                    cityAdapter.setBeans(beans);
                    response.success(cityAdapter);
                } else {
                    response.fail(geoBean.getCode().getTxt());
                }
            }
        });
    }
}
